﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;

namespace DeThiTracNghiem
{
    public partial class formTaoDeThi : Form
    {
        public formTaoDeThi()
        {
            InitializeComponent();
        }

        private CauHoiTracNghiem_LinQDataContext data = new CauHoiTracNghiem_LinQDataContext();

        private void formTaoDeThi_Load(object sender, EventArgs e)
        {
            LoadDeThi();
        }

        private void LoadDeThi()
        {
            listView1.Groups.Clear();
            listView1.Items.Clear();

            var dschude = data.ChuDes.Select(cd => cd);
            int stt = 0;
            foreach (var cd in dschude)
            {
                ListViewGroup lvg = new ListViewGroup();
                lvg.Header = cd.TenChuDe;
                listView1.Groups.Add(lvg);
               
                var dscauhoi = from ch in data.CauHois
                               where ch.MaChuDe == cd.MaChuDe
                               select ch;
                foreach (var ch in dscauhoi)
                {
                    ListViewItem lvi = new ListViewItem();
                    stt++;
                    lvi.Text = stt.ToString();
                    lvi.SubItems.Add(ch.NoiDung );
                    lvi.Group = lvg;
                    lvi.Tag = ch.MaCauHoi;
                    listView1.Items.Add(lvi);
                }
            }
        }

        private void buttonTaoDeThi_Click(object sender, EventArgs e)
        {
            if (listView1.CheckedItems.Count > 0 && TextBoxThoiGian.Text != "")
            {

                string src;
                SaveFileDialog save = new SaveFileDialog();
                save.Filter = ".xml|*.xml";
                if (save.ShowDialog() == DialogResult.OK)
                {
                    src = save.FileName;

                    XDocument doc = new XDocument();
                    XComment comment = new XComment("DE THI");
                    doc.Add(comment);

                    XElement root = new XElement("DanhSach");
                    XAttribute thoigian = new XAttribute("ThoiGian", TextBoxThoiGian.Text);
                    root.Add(thoigian);
                    doc.Add(root);

                    foreach (ListViewItem lvi in listView1.CheckedItems)
                    {
                        int macauhoi = int.Parse(lvi.Tag.ToString());
                        var ch = data.CauHois.Single(c => c.MaCauHoi == macauhoi);

                        XElement cauhoi = new XElement("CauHoi");
                        XAttribute chude = new XAttribute("ChuDe", ch.MaChuDe);
                        cauhoi.Add(chude);

                        XElement noidung = new XElement("NoiDung", ch.NoiDung);
                        XElement dapana = new XElement("DapAnA", ch.DapAnA);
                        XElement dapanb = new XElement("DapAnB", ch.DapAnB);
                        XElement dapanc = new XElement("DapAnC", ch.DapAnC);
                        XElement dapand = new XElement("DapAnD", ch.DapAnD);
                        XElement dapandung = new XElement("DapAnDung", ch.DapAnDung);

                        cauhoi.Add(noidung);
                        cauhoi.Add(dapana);
                        cauhoi.Add(dapanb);
                        cauhoi.Add(dapanc);
                        cauhoi.Add(dapand);
                        cauhoi.Add(dapandung);

                        root.Add(cauhoi);
                    }
                    doc.Save(src);
                }
            }
            else
                MessageBox.Show("Chưa chọn câu hỏi hoặc thời gian thi");
        }

        private void buttonThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
